package com.example.springboot01.webcontroller;

import com.example.springboot01.service.UserInfoSerivce;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import com.example.springboot01.entity.ProInfo;
import com.example.springboot01.entity.UserInfo;
import com.example.springboot01.service.ProInfoService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Controller
public class UserInfoController {
    @Autowired
    private UserInfoSerivce userInfoSerivce;
    @Autowired
    private ProInfoService proInfoService;

    @RequestMapping("qry_user_list")
    public String qryUserList(HttpServletRequest request, Model model){
        String field_name = request.getParameter("field_name");
        String field_value = request.getParameter("qry_content");
        System.out.println("controller: field_name--" + field_name
                + "---field_value---" + field_value);
        if (field_name == "" ||field_name.equals(null)) {
            field_name = "user_code";
            field_value = "";
        }

        //把查询的工作交给service去做。自己只想得到service返回的结果
        List<UserInfo> user_list = userInfoSerivce.qryUserList(field_name, field_value);
        System.out.println("controller：数据集数量：" + user_list.size());
        model.addAttribute("user_list", user_list);

        return "user_list";
    }

    @RequestMapping("add_user")
    public String showAddUser(Model model){
        List<ProInfo> proInfoList = proInfoService.getProInfoList();
        model.addAttribute("pro_list", proInfoList);

        return "add_user";
    }
    @RequestMapping("insert_a_user")//不用动态绑定的的方式
    public String insertAUser(HttpServletRequest request, RedirectAttributes attributes){
        int flag = 0;
        UserInfo userInfo = new UserInfo();
        userInfo.setUser_code(request.getParameter("user_code"));
        userInfo.setUser_name(request.getParameter("user_name"));
        userInfo.setUser_pwd("123456");
        userInfo.setTelephone(request.getParameter("telephone"));
        userInfo.setUser_pro_id(Integer.parseInt(request.getParameter("user_pro_id")));
        System.out.println("controller:" + userInfo.toString());
        flag = userInfoSerivce.insertAUser(userInfo);

        if (flag >0) {
            attributes.addAttribute("field_name", "user_code");
            attributes.addAttribute("qry_content","");
            return "redirect:qry_user_list";
        }
        else
            return "error";
    }
    @RequestMapping("insert_a_user_binding")//动态自动绑定的的方式
    public String insertAUserBinding(UserInfo userInfo, RedirectAttributes attr){
        int flag = 0;
        System.out.println("controller:" + userInfo.toString());
        flag = userInfoSerivce.insertAUser(userInfo);

        if (flag > 0) {
            attr.addAttribute("field_name", "user_code");
            attr.addAttribute("qry_content","");
            return "redirect:qry_user_list";
        }
        else
            return "error";
    }
    @RequestMapping("delete_user")//一般都是假删除，通过update完成。很少用delete真删除
    public String deleteUser(HttpServletRequest request, RedirectAttributes attributes){
        int flag = 0;
        String user_code = request.getParameter("user_code");
        flag = userInfoSerivce.deleteUserInfo(user_code);

        if (flag > 0) {
            attributes.addAttribute("field_name", "user_code");
            attributes.addAttribute("qry_content","");
            return "redirect:qry_user_list";
        }
        else
            return "error";
    }

    @RequestMapping("show_edit_user")
    public String showEditUser(HttpServletRequest request, Model model){
        String user_code = request.getParameter("user_code");
        //根据传入的主键让service获取UserInfo类的对象并返回给userInfo。
        UserInfo userInfo = userInfoSerivce.getAUserInfo(user_code);
        List<ProInfo> proInfoList = proInfoService.getProInfoList();
        model.addAttribute("user_info", userInfo);
        model.addAttribute("pro_list", proInfoList);

        return "edit_user";//templates下面的同名网页
    }

    //    @RequestMapping("edit_user")
//    public String editUser(HttpServletRequest request){
//        return "";
//    }
    @RequestMapping("edit_user")
    //动态绑定的方式，userInfo能自动接受网页上表单的参数名和值，并自动转换成UserInfo对象里面的值
    public String editUser(UserInfo userInfo, RedirectAttributes attributes){
        System.out.println("controller:" + userInfo.toString());
        int flag = 0;
        flag = userInfoSerivce.editUser(userInfo);
        if (flag > 0){
            attributes.addAttribute("field_name", "user_code");
            attributes.addAttribute("qry_content","");
            return "redirect:qry_user_list";
        }
        else
            return "error";
    }

    @RequestMapping("qry_user_page_list")
    public String qryUserPageList(HttpServletRequest request, Model model){
        String field_name = request.getParameter("field_name");
        String qry_content = request.getParameter("qry_content");
        int start_page = Integer.parseInt(request.getParameter("start_page"));
        if (start_page <= 0)
            start_page = 1;
        int page_size = Integer.parseInt(request.getParameter("page_size"));
        System.out.println(field_name + "@" + qry_content + "@" +
                start_page + "@" + page_size);
        List<UserInfo> user_list = userInfoSerivce.qryUserPageList(field_name,
                qry_content, start_page, page_size);
        model.addAttribute("user_list", user_list);
        //model不仅可以传数据集，也可以传单个对象或者变量。
        model.addAttribute("field_name", field_name);
        model.addAttribute("qry_content", qry_content);
        model.addAttribute("start_page", start_page);
        model.addAttribute("page_size", page_size);
        return "user_page_list";
    }

    @RequestMapping("show_main")
    public String showMain(){
        return "main";
    }
    @RequestMapping("form_array")
    public String formArray(){
        return "form_array";
    }
    @RequestMapping("receive_arr")
    public String receiveArr(@RequestParam("info") String info){

        return "main";
    }
}
